package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import ass.beans.CinemaMovieBean;
import ass.beans.MovieBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;

public class CinemaMovieDAOImpl {
	
	/**
	 * The service locator to retrieve database connections from
	 */
	private DBConnectionFactory services;
	

	public CinemaMovieDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public CinemaMovieDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}
	
	public void insert(CinemaMovieBean bean) throws DataAccessException {
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO TBL_CINEMA_MOVIE (CINEMA_ID, MOVIE_ID,SHOWTIME) values ( ?, ?, ?)");
	     
	     stmt.setInt(1, bean.getCinema_id());
	     stmt.setInt(2, bean.getMovie_id());	    		 
	     stmt.setTimestamp(3, bean.getShowtime());
	
	     
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
	}

	public List<CinemaMovieBean> getShowTimeByMovieID(int movie_id) {
		
		List<CinemaMovieBean> list = new ArrayList<CinemaMovieBean>();
		Connection con = null;
		try {
			con = services.createConnection();
			PreparedStatement stmt = con
					.prepareStatement("select * from TBL_CINEMA_MOVIE where MOVIE_ID =?");
			stmt.setInt(1, movie_id);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				CinemaMovieBean cinemaMovieBean = createCinemaMovieBean(rs);
				list.add(cinemaMovieBean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
	}

	private CinemaMovieBean createCinemaMovieBean(ResultSet rs) throws SQLException {
		
//		CINEMA_ID INTEGER,
//		MOVIE_ID INTEGER,
//		SHOWTIME DATE
		
		CinemaMovieBean cinemaMovieBean = new CinemaMovieBean();

		cinemaMovieBean.setMovie_id(rs.getInt("movie_id"));
		cinemaMovieBean.setCinema_id(rs.getInt("cinema_id"));
		cinemaMovieBean.setShowtime(rs.getTimestamp("showtime"));


		return cinemaMovieBean;
	}



}

